package org.esprit.ocm.client.ui.ec2;

import org.esprit.ocm.client.rpc.ec2.RegistrationService;
import org.esprit.ocm.client.rpc.ec2.RegistrationServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class RegisterImage extends Window{

	private final RegistrationServiceAsync	registrationService	= GWT.create(RegistrationService.class);
	
	public RegisterImage(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {
		
		setTitle("Register Image");
		setWidth(250);
		setHeight(100);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		centerInPage();

		final DynamicForm form = new DynamicForm();
		form.setWidth(100);
		form.setHeight(50);
		form.setPadding(5);
		form.setLayoutAlign(VerticalAlignment.TOP);

		final TextItem manifest = new TextItem("manifest");
		manifest.setTitle("Manifest");
		manifest.setRequired(true);

		ButtonItem createButton = new ButtonItem("register", "Register");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				
				registrationService.registerImage(manifest.getValue().toString(), server, credentials, new AsyncCallback<String>() {
					
					public void onSuccess(String s) {

						hide();
						SC.say("Registration: " + s);
					}
					
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});

		form.setFields(manifest, createButton);
		form.redraw();

		addItem(form);

		draw();
	}
}
